
using System;
using System.Reflection;
using System.Collections;
using UniversalStorageServices;
using UniversalStorageServices.PersistenceManager;

		

namespace Example
{
		
	public interface IEmployee
	{
		string Id { get; set; }
		System.String Name { get; set; }
		System.String Firstname { get; set; }
		System.DateTime Birthday { get; set; }
		
	}
	
	
		/// <summary>
	/// <para>
	/// A collection that stores <see cref='Employee'/> objects.
	/// </para>
	/// </summary>
	/// <seealso cref='EmployeeCollection'/>
	[Serializable()]
	public class  EmployeeCollection : IPersistableCollection 
	{	
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='EmployeeCollection'/>.
		/// </para>
		/// </summary>
		public EmployeeCollection()
		{
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='EmployeeCollection'/> based on another <see cref='EmployeeCollection'/>.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='EmployeeCollection'/> from which the contents are copied
		/// </param>
		public EmployeeCollection( EmployeeCollection val)
		{
			this.AddRange(val);	
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='EmployeeCollection'/> containing any array of <see cref='Employee'/> objects.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A array of <see cref='Employee'/> objects with which to intialize the collection
		/// </param>
		public EmployeeCollection( Employee[] val)
		{
			this.AddRange(val);
		}
		
		public EmployeeCollection (Entity parentEntity, string role, IPersistableCollection data): base(parentEntity, role, data)
		{
		}
		
		/// <summary>
		/// <para>Represents the entry at the specified index of the <see cref='Employee'/>.</para>
		/// </summary>
		/// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>
		/// <value>
		/// <para> The entry at the specified index of the collection.</para>
		/// </value>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public new Employee this[int index] 
		{
			get 
			{
				return ((Employee)base[index]);
			}
			set 
			{
				base[index] = value;
			}
		}
		
		/// <summary>
		/// <para>Adds a <see cref='Employee'/> with the specified value to the 
		/// <see cref='EmployeeCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Employee'/> to add.</param>
		/// <returns>
		/// <para>The index at which the new element was inserted.</para>
		/// </returns>
		/// <seealso cref='EmployeeCollection.AddRange'/>
		public int Add( Employee val)
		{
			return base.Add(val);
		}
		
	
		
		/// <summary>
		/// <para>
		/// Adds the contents of another <see cref='EmployeeCollection'/> to the end of the collection.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='EmployeeCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <seealso cref='EmployeeCollection.Add'/>
		public void AddRange( EmployeeCollection val)
		{
			base.AddRange(val);
		}
		public void AddRange( Employee[] val)
		{
			base.AddRange(val);
		}
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		/// <see cref='EmployeeCollection'/> contains the specified <see cref='Employee'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='Employee'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='Employee'/> is contained in the collection; 
		/// otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='EmployeeCollection.IndexOf'/>
		public bool Contains( Employee val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		/// <para>Copies the <see cref='EmployeeCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the 
		/// specified index.</para>
		/// </summary>
		/// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='EmployeeCollection'/> .</para></param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='EmployeeCollection'/> is greater than the available space between <paramref name='index'/> and the end of <paramref name='array'/>.</para></exception>
		/// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='System.Array'/>
		public void CopyTo( Employee[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		/// <para>Returns the index of a <see cref='Employee'/> in 
		/// the <see cref='EmployeeCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Employee'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='Employee'/> of <paramref name='val'/> in the 
		/// <see cref='EmployeeCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		/// <seealso cref='EmployeeCollection.Contains'/>
		public int IndexOf( Employee val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		/// <para>Inserts a <see cref='Employee'/> into the <see cref='EmployeeCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='Employee'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='EmployeeCollection.Add'/>
		public void Insert(int index,  Employee val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		/// <para>Returns an enumerator that can iterate through 
		/// the <see cref='EmployeeCollection'/> .</para>
		/// </summary>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='System.Collections.IEnumerator'/>
		public new  EmployeeEnumerator GetEnumerator()
		{
			return new  EmployeeEnumerator(this);
		}
		
		/// <summary>
		/// <para> Removes a specific <see cref='Employee'/> from the 
		/// <see cref='EmployeeCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Employee'/> to remove from the <see cref='EmployeeCollection'/> .</param>
		/// <returns><para>None.</para></returns>
		/// <exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>
		public void Remove( Employee val)
		{
			List.Remove(val);
		}
		
		public class  EmployeeEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			public  EmployeeEnumerator( EmployeeCollection mappings)
			{
				this.temp = mappings;
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			public  Employee Current 
			{
				get 
				{
					return (( Employee)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current 
			{
				get 
				{
					return baseEnumerator.Current;
				}
			}
			
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			bool IEnumerator.MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			public void Reset()
			{
				baseEnumerator.Reset();
			}
			
			void IEnumerator.Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}

	
	[Serializable]
	public class Employee :  IPersistable, IEmployee 
	{		
	
		protected Entity _Entity;
		
		[NonSerialized]
		protected PersistenceManager _PersistenceManager;
		
		Entity IPersistable.Entity
		{
			get
			{
				return _Entity;
			}
			set
			{
				_Entity = value;
			}
		}
		
		PersistenceManager IPersistable.PersistenceManager
		{
			get
			{
				return _PersistenceManager;
			}
			set
			{
				_PersistenceManager = value;
			}
		}
		
		public string Id
		{
			get
			{
				return _Entity.Id;
			}
			set
			{
				_Entity.Id = value;
			}
		}
		
		~Employee()
		{
			if(_PersistenceManager != null)
				_PersistenceManager.RemoveFromCache(this);
		}
	
		void IPersistable.TrackChildren()
		{
			// if the object is not tracked there is nothing to do
			if(_PersistenceManager == null)
				return;
	
		}
		
		void IPersistable.DeleteChildren()
		{	
	
		}
	
		public System.String Name
		{
			get 
			{ 
				if(_Entity.GetValue("Name") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Name"]; 
			}
		
			set 
			{ 
				this._Entity["Name"] = value; 
			}
		}
		
		public System.String Firstname
		{
			get 
			{ 
				if(_Entity.GetValue("Firstname") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Firstname"]; 
			}
		
			set 
			{ 
				this._Entity["Firstname"] = value; 
			}
		}
		
		public System.DateTime Birthday
		{
			get 
			{ 
				if(_Entity.GetValue("Birthday") == null)
					return (new System.DateTime[1])[0];
				else
					return (System.DateTime)this._Entity["Birthday"]; 
			}
		
			set 
			{ 
				this._Entity["Birthday"] = value; 
			}
		}
		
		public Employee ()
		{
			
			this._Entity = new Entity(PersistenceManager.ConvertNamespaceDomainToEuss(this.GetType().FullName));
		}

		public Employee (Entity e): this()
		{
			this._Entity = e;
		}

		private ParentRelationships _Parents;

		public ParentRelationships Parents
		{
			get
			{
				if (_Parents == null)
					if (_PersistenceManager == null)
						throw new ApplicationException("Can't call Parents untracked objects");
				else
					_Parents = new ParentRelationships(this);

				return _Parents;
			}
		}

		public class ParentRelationships
		{
			private IPersistable _Owner;
	
			public ParentRelationships(IPersistable o)
			{
				_Owner = o;
			}
		
			public Job GetCompanyEmployeesJob()
			{
				IPersistableCollection items = _Owner.PersistenceManager.Load(typeof(Job), String.Format("[count(Employee[Id = '{0}']) > 0]", _Owner.Id));
			
				if (items.Count > 0)
					return (Job)items[0];
				else
					return null;
			}
	
		}
	}
	
	public interface ICompany
	{
		string Id { get; set; }
		System.String Name { get; set; }
		System.String Chairman { get; set; }
		System.String Address { get; set; }
		System.String City { get; set; }
		
		IJob[]  EmployeeJob { get; }

	}
	
	
		/// <summary>
	/// <para>
	/// A collection that stores <see cref='Company'/> objects.
	/// </para>
	/// </summary>
	/// <seealso cref='CompanyCollection'/>
	[Serializable()]
	public class  CompanyCollection : IPersistableCollection 
	{	
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='CompanyCollection'/>.
		/// </para>
		/// </summary>
		public CompanyCollection()
		{
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='CompanyCollection'/> based on another <see cref='CompanyCollection'/>.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='CompanyCollection'/> from which the contents are copied
		/// </param>
		public CompanyCollection( CompanyCollection val)
		{
			this.AddRange(val);	
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='CompanyCollection'/> containing any array of <see cref='Company'/> objects.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A array of <see cref='Company'/> objects with which to intialize the collection
		/// </param>
		public CompanyCollection( Company[] val)
		{
			this.AddRange(val);
		}
		
		public CompanyCollection (Entity parentEntity, string role, IPersistableCollection data): base(parentEntity, role, data)
		{
		}
		
		/// <summary>
		/// <para>Represents the entry at the specified index of the <see cref='Company'/>.</para>
		/// </summary>
		/// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>
		/// <value>
		/// <para> The entry at the specified index of the collection.</para>
		/// </value>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public new Company this[int index] 
		{
			get 
			{
				return ((Company)base[index]);
			}
			set 
			{
				base[index] = value;
			}
		}
		
		/// <summary>
		/// <para>Adds a <see cref='Company'/> with the specified value to the 
		/// <see cref='CompanyCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Company'/> to add.</param>
		/// <returns>
		/// <para>The index at which the new element was inserted.</para>
		/// </returns>
		/// <seealso cref='CompanyCollection.AddRange'/>
		public int Add( Company val)
		{
			return base.Add(val);
		}
		
	
		
		/// <summary>
		/// <para>
		/// Adds the contents of another <see cref='CompanyCollection'/> to the end of the collection.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='CompanyCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <seealso cref='CompanyCollection.Add'/>
		public void AddRange( CompanyCollection val)
		{
			base.AddRange(val);
		}
		public void AddRange( Company[] val)
		{
			base.AddRange(val);
		}
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		/// <see cref='CompanyCollection'/> contains the specified <see cref='Company'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='Company'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='Company'/> is contained in the collection; 
		/// otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='CompanyCollection.IndexOf'/>
		public bool Contains( Company val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		/// <para>Copies the <see cref='CompanyCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the 
		/// specified index.</para>
		/// </summary>
		/// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='CompanyCollection'/> .</para></param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='CompanyCollection'/> is greater than the available space between <paramref name='index'/> and the end of <paramref name='array'/>.</para></exception>
		/// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='System.Array'/>
		public void CopyTo( Company[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		/// <para>Returns the index of a <see cref='Company'/> in 
		/// the <see cref='CompanyCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Company'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='Company'/> of <paramref name='val'/> in the 
		/// <see cref='CompanyCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		/// <seealso cref='CompanyCollection.Contains'/>
		public int IndexOf( Company val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		/// <para>Inserts a <see cref='Company'/> into the <see cref='CompanyCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='Company'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='CompanyCollection.Add'/>
		public void Insert(int index,  Company val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		/// <para>Returns an enumerator that can iterate through 
		/// the <see cref='CompanyCollection'/> .</para>
		/// </summary>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='System.Collections.IEnumerator'/>
		public new  CompanyEnumerator GetEnumerator()
		{
			return new  CompanyEnumerator(this);
		}
		
		/// <summary>
		/// <para> Removes a specific <see cref='Company'/> from the 
		/// <see cref='CompanyCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Company'/> to remove from the <see cref='CompanyCollection'/> .</param>
		/// <returns><para>None.</para></returns>
		/// <exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>
		public void Remove( Company val)
		{
			List.Remove(val);
		}
		
		public class  CompanyEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			public  CompanyEnumerator( CompanyCollection mappings)
			{
				this.temp = mappings;
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			public  Company Current 
			{
				get 
				{
					return (( Company)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current 
			{
				get 
				{
					return baseEnumerator.Current;
				}
			}
			
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			bool IEnumerator.MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			public void Reset()
			{
				baseEnumerator.Reset();
			}
			
			void IEnumerator.Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}

	
	[Serializable]
	public class Company :  IPersistable, ICompany 
	{		
	
		protected Entity _Entity;
		
		[NonSerialized]
		protected PersistenceManager _PersistenceManager;
		
		Entity IPersistable.Entity
		{
			get
			{
				return _Entity;
			}
			set
			{
				_Entity = value;
			}
		}
		
		PersistenceManager IPersistable.PersistenceManager
		{
			get
			{
				return _PersistenceManager;
			}
			set
			{
				_PersistenceManager = value;
			}
		}
		
		public string Id
		{
			get
			{
				return _Entity.Id;
			}
			set
			{
				_Entity.Id = value;
			}
		}
		
		~Company()
		{
			if(_PersistenceManager != null)
				_PersistenceManager.RemoveFromCache(this);
		}
	
		void IPersistable.TrackChildren()
		{
			// if the object is not tracked there is nothing to do
			if(_PersistenceManager == null)
				return;
	
			if(_EmployeeJob != null)
				_PersistenceManager.TrackObject(_EmployeeJob);
		
		}
		
		void IPersistable.DeleteChildren()
		{	
	
		}
	
		public System.String Name
		{
			get 
			{ 
				if(_Entity.GetValue("Name") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Name"]; 
			}
		
			set 
			{ 
				this._Entity["Name"] = value; 
			}
		}
		
		public System.String Chairman
		{
			get 
			{ 
				if(_Entity.GetValue("Chairman") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Chairman"]; 
			}
		
			set 
			{ 
				this._Entity["Chairman"] = value; 
			}
		}
		
		public System.String Address
		{
			get 
			{ 
				if(_Entity.GetValue("Address") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Address"]; 
			}
		
			set 
			{ 
				this._Entity["Address"] = value; 
			}
		}
		
		public System.String City
		{
			get 
			{ 
				if(_Entity.GetValue("City") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["City"]; 
			}
		
			set 
			{ 
				this._Entity["City"] = value; 
			}
		}
		
		protected JobCollection _EmployeeJob;
	
		public JobCollection  EmployeeJob
		{
			get
			{
				if (_EmployeeJob == null)
				{
					if(_PersistenceManager == null) // Standalone collection
						_EmployeeJob = new JobCollection(_Entity, "EmployeeJob", new IPersistableCollection());
					else
					{
						IPersistableCollection refs = _PersistenceManager.LoadReference(_Entity, "EmployeeJob") ;
						_EmployeeJob = new JobCollection(_Entity, "EmployeeJob", refs) ;
					}
				}
				return _EmployeeJob;
			}	

			set
			{
				_EmployeeJob = value;
			}
		}	

		IJob[] ICompany.EmployeeJob
		{	                
			get 
			{ 
				IPersistable[] result = new IPersistable[EmployeeJob.Count];
				EmployeeJob.CopyTo(result, 0);
				return (IJob[])result; 
			}
		}
	
		public Company ()
		{
			
			this._Entity = new Entity(PersistenceManager.ConvertNamespaceDomainToEuss(this.GetType().FullName));
		}

		public Company (Entity e): this()
		{
			this._Entity = e;
		}

		private ParentRelationships _Parents;

		public ParentRelationships Parents
		{
			get
			{
				if (_Parents == null)
					if (_PersistenceManager == null)
						throw new ApplicationException("Can't call Parents untracked objects");
				else
					_Parents = new ParentRelationships(this);

				return _Parents;
			}
		}

		public class ParentRelationships
		{
			private IPersistable _Owner;
	
			public ParentRelationships(IPersistable o)
			{
				_Owner = o;
			}
		
		}
	}
	
	public interface IJob
	{
		string Id { get; set; }
		System.String Title { get; set; }
		System.DateTime StartDate { get; set; }
		
		IEmployee Employee { get; }
		
	}
	
	
		/// <summary>
	/// <para>
	/// A collection that stores <see cref='Job'/> objects.
	/// </para>
	/// </summary>
	/// <seealso cref='JobCollection'/>
	[Serializable()]
	public class  JobCollection : IPersistableCollection 
	{	
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='JobCollection'/>.
		/// </para>
		/// </summary>
		public JobCollection()
		{
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='JobCollection'/> based on another <see cref='JobCollection'/>.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='JobCollection'/> from which the contents are copied
		/// </param>
		public JobCollection( JobCollection val)
		{
			this.AddRange(val);	
		}
		
		/// <summary>
		/// <para>
		/// Initializes a new instance of <see cref='JobCollection'/> containing any array of <see cref='Job'/> objects.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A array of <see cref='Job'/> objects with which to intialize the collection
		/// </param>
		public JobCollection( Job[] val)
		{
			this.AddRange(val);
		}
		
		public JobCollection (Entity parentEntity, string role, IPersistableCollection data): base(parentEntity, role, data)
		{
		}
		
		/// <summary>
		/// <para>Represents the entry at the specified index of the <see cref='Job'/>.</para>
		/// </summary>
		/// <param name='index'><para>The zero-based index of the entry to locate in the collection.</para></param>
		/// <value>
		/// <para> The entry at the specified index of the collection.</para>
		/// </value>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is outside the valid range of indexes for the collection.</exception>
		public new Job this[int index] 
		{
			get 
			{
				return ((Job)base[index]);
			}
			set 
			{
				base[index] = value;
			}
		}
		
		/// <summary>
		/// <para>Adds a <see cref='Job'/> with the specified value to the 
		/// <see cref='JobCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Job'/> to add.</param>
		/// <returns>
		/// <para>The index at which the new element was inserted.</para>
		/// </returns>
		/// <seealso cref='JobCollection.AddRange'/>
		public int Add( Job val)
		{
			return base.Add(val);
		}
		
	
		
		/// <summary>
		/// <para>
		/// Adds the contents of another <see cref='JobCollection'/> to the end of the collection.
		/// </para>
		/// </summary>
		/// <param name='val'>
		/// A <see cref='JobCollection'/> containing the objects to add to the collection.
		/// </param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <seealso cref='JobCollection.Add'/>
		public void AddRange( JobCollection val)
		{
			base.AddRange(val);
		}
		public void AddRange( Job[] val)
		{
			base.AddRange(val);
		}
		
		/// <summary>
		/// <para>Gets a value indicating whether the 
		/// <see cref='JobCollection'/> contains the specified <see cref='Job'/>.</para>
		/// </summary>
		/// <param name='val'>The <see cref='Job'/> to locate.</param>
		/// <returns>
		/// <para><see langword='true'/> if the <see cref='Job'/> is contained in the collection; 
		/// otherwise, <see langword='false'/>.</para>
		/// </returns>
		/// <seealso cref='JobCollection.IndexOf'/>
		public bool Contains( Job val)
		{
			return List.Contains(val);
		}
		
		/// <summary>
		/// <para>Copies the <see cref='JobCollection'/> values to a one-dimensional <see cref='System.Array'/> instance at the 
		/// specified index.</para>
		/// </summary>
		/// <param name='array'><para>The one-dimensional <see cref='System.Array'/> that is the destination of the values copied from <see cref='JobCollection'/> .</para></param>
		/// <param name='index'>The index in <paramref name='array'/> where copying begins.</param>
		/// <returns>
		/// <para>None.</para>
		/// </returns>
		/// <exception cref='System.ArgumentException'><para><paramref name='array'/> is multidimensional.</para> <para>-or-</para> <para>The number of elements in the <see cref='JobCollection'/> is greater than the available space between <paramref name='index'/> and the end of <paramref name='array'/>.</para></exception>
		/// <exception cref='System.ArgumentNullException'><paramref name='array'/> is <see langword='null'/>. </exception>
		/// <exception cref='System.ArgumentOutOfRangeException'><paramref name='index'/> is less than <paramref name='array'/>'s lowbound. </exception>
		/// <seealso cref='System.Array'/>
		public void CopyTo( Job[] array, int index)
		{
			List.CopyTo(array, index);
		}
		
		/// <summary>
		/// <para>Returns the index of a <see cref='Job'/> in 
		/// the <see cref='JobCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Job'/> to locate.</param>
		/// <returns>
		/// <para>The index of the <see cref='Job'/> of <paramref name='val'/> in the 
		/// <see cref='JobCollection'/>, if found; otherwise, -1.</para>
		/// </returns>
		/// <seealso cref='JobCollection.Contains'/>
		public int IndexOf( Job val)
		{
			return List.IndexOf(val);
		}
		
		/// <summary>
		/// <para>Inserts a <see cref='Job'/> into the <see cref='JobCollection'/> at the specified index.</para>
		/// </summary>
		/// <param name='index'>The zero-based index where <paramref name='val'/> should be inserted.</param>
		/// <param name='val'>The <see cref='Job'/> to insert.</param>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='JobCollection.Add'/>
		public void Insert(int index,  Job val)
		{
			List.Insert(index, val);
		}
		
		/// <summary>
		/// <para>Returns an enumerator that can iterate through 
		/// the <see cref='JobCollection'/> .</para>
		/// </summary>
		/// <returns><para>None.</para></returns>
		/// <seealso cref='System.Collections.IEnumerator'/>
		public new  JobEnumerator GetEnumerator()
		{
			return new  JobEnumerator(this);
		}
		
		/// <summary>
		/// <para> Removes a specific <see cref='Job'/> from the 
		/// <see cref='JobCollection'/> .</para>
		/// </summary>
		/// <param name='val'>The <see cref='Job'/> to remove from the <see cref='JobCollection'/> .</param>
		/// <returns><para>None.</para></returns>
		/// <exception cref='System.ArgumentException'><paramref name='val'/> is not found in the Collection. </exception>
		public void Remove( Job val)
		{
			List.Remove(val);
		}
		
		public class  JobEnumerator : IEnumerator
		{
			IEnumerator baseEnumerator;
			IEnumerable temp;
			
			public  JobEnumerator( JobCollection mappings)
			{
				this.temp = mappings;
				this.baseEnumerator = temp.GetEnumerator();
			}
			
			public  Job Current 
			{
				get 
				{
					return (( Job)(baseEnumerator.Current));
				}
			}
			
			object IEnumerator.Current 
			{
				get 
				{
					return baseEnumerator.Current;
				}
			}
			
			public bool MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			bool IEnumerator.MoveNext()
			{
				return baseEnumerator.MoveNext();
			}
			
			public void Reset()
			{
				baseEnumerator.Reset();
			}
			
			void IEnumerator.Reset()
			{
				baseEnumerator.Reset();
			}
		}
	}

	
	[Serializable]
	public class Job :  IPersistable, IJob 
	{		
	
		protected Entity _Entity;
		
		[NonSerialized]
		protected PersistenceManager _PersistenceManager;
		
		Entity IPersistable.Entity
		{
			get
			{
				return _Entity;
			}
			set
			{
				_Entity = value;
			}
		}
		
		PersistenceManager IPersistable.PersistenceManager
		{
			get
			{
				return _PersistenceManager;
			}
			set
			{
				_PersistenceManager = value;
			}
		}
		
		public string Id
		{
			get
			{
				return _Entity.Id;
			}
			set
			{
				_Entity.Id = value;
			}
		}
		
		~Job()
		{
			if(_PersistenceManager != null)
				_PersistenceManager.RemoveFromCache(this);
		}
	
		void IPersistable.TrackChildren()
		{
			// if the object is not tracked there is nothing to do
			if(_PersistenceManager == null)
				return;
	
			if(_Employee != null)
				_PersistenceManager.TrackObject(_Employee);
		
		}
		
		void IPersistable.DeleteChildren()
		{	
	
		}
	
		public System.String Title
		{
			get 
			{ 
				if(_Entity.GetValue("Title") == null)
					return (new System.String[1])[0];
				else
					return (System.String)this._Entity["Title"]; 
			}
		
			set 
			{ 
				this._Entity["Title"] = value; 
			}
		}
		
		public System.DateTime StartDate
		{
			get 
			{ 
				if(_Entity.GetValue("StartDate") == null)
					return (new System.DateTime[1])[0];
				else
					return (System.DateTime)this._Entity["StartDate"]; 
			}
		
			set 
			{ 
				this._Entity["StartDate"] = value; 
			}
		}
		
		private bool _IsEmployeeLoaded = false;

		private Employee _Employee;

		public Employee Employee
		{
			get 
			{ 
				if(!_IsEmployeeLoaded && _PersistenceManager != null)
				{
					IPersistableCollection items = _PersistenceManager.LoadReference(_Entity, "Employee");
					if(items.Count > 0)
						_Employee = (Employee)items[0];
				}
				
				_IsEmployeeLoaded = true;
				return _Employee;
			}
			set
			{
				// Removes the reference to an existing entity
				if(Employee != null)
				{
					_Entity.DeleteElement("Employee");
				}
				
				// Creates the reference only if it is a concrete entity
				if(value != null)
				{
					_Entity.AddValue("Employee", ((IPersistable)value).Entity);
				}
				_Employee = value;
			}
		}				
			
		IEmployee IJob.Employee
		{
			get { return Employee; }
		}
		
		public Job ()
		{
			
			this._Entity = new Entity(PersistenceManager.ConvertNamespaceDomainToEuss(this.GetType().FullName));
		}

		public Job (Entity e): this()
		{
			this._Entity = e;
		}

		private ParentRelationships _Parents;

		public ParentRelationships Parents
		{
			get
			{
				if (_Parents == null)
					if (_PersistenceManager == null)
						throw new ApplicationException("Can't call Parents untracked objects");
				else
					_Parents = new ParentRelationships(this);

				return _Parents;
			}
		}

		public class ParentRelationships
		{
			private IPersistable _Owner;
	
			public ParentRelationships(IPersistable o)
			{
				_Owner = o;
			}
		
			public Company GetCompanyEmployees()
			{
				IPersistableCollection items = _Owner.PersistenceManager.Load(typeof(Company), String.Format("[count(EmployeeJob[Id = '{0}']) > 0]", _Owner.Id));
			
				if (items.Count > 0)
					return (Company)items[0];
				else
					return null;
			}
	
		}
	}

}
